package com.tianqu.tms.www.settle.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferSettlementInfoDao;
import com.tianqu.tms.dto.TransferSettlementInfoDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleSearchDatailAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
		String insertUserId = "";
		if(request.getParameter("userid")!=null)
		{
			insertUserId = request.getParameter("userid");
		}else{
			insertUserId = (String)uc.getAttribute("settleInsertUserId");
		}
		TransferSettleViewHelper view = (TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
		if(!"0".equals(insertUserId))
		{
			User user = new User();
			user = user.getUserInfoById(Integer.parseInt(insertUserId));
			view.setChargeName(user.getRealName());
			
			TransferSettlementDate settleDate =new TransferSettlementDate();
			settleDate = settleDate.getDateInfoBySettleId(view.getSettleId(),Integer.parseInt(insertUserId));
			view.setStartDate(settleDate.getStartDate());
			view.setEndDate(settleDate.getEndDate());
		}
		if(uc.getAttribute("tsxx")!=null)
		{
			view.setTishixinxi("1");
			uc.removeAttribute("tsxx");
		}else{
			view.setTishixinxi("");
		}
//		//流程表收费
//		TransferSettlementInfo getYPrice1 = new TransferSettlementInfo();
//		getYPrice1 = getYPrice1.getYPriceByUserId(insertUserId,1,view.getSettleId());
//		view.setYprice1(StringUtil.addDH(String.valueOf(getYPrice1.getYsPrice())));
//		view.setSprice1(StringUtil.addDH(String.valueOf(getYPrice1.getSsPrice())));
//		//交易票收费
//		TransferSettlementInfo getYPrice2 = new TransferSettlementInfo();
//		getYPrice2 = getYPrice2.getYPriceByUserId(insertUserId,3,view.getSettleId());
//		view.setYprice2(StringUtil.addDH(String.valueOf(getYPrice2.getYsPrice())));
//		view.setSprice2(StringUtil.addDH(String.valueOf(getYPrice2.getSsPrice())));
//		//待办收费
//		TransferSettlementInfo getYPrice3 = new TransferSettlementInfo();
//		getYPrice3 = getYPrice3.getYPriceByUserId(insertUserId,6,view.getSettleId());
//		view.setYprice3(StringUtil.addDH(String.valueOf(getYPrice3.getYsPrice())));
//		view.setSprice3(StringUtil.addDH(String.valueOf(getYPrice3.getSsPrice())));
//		//待办超期/过期收费
//		TransferSettlementInfo getYPrice4 = new TransferSettlementInfo();
//		getYPrice4 = getYPrice4.getYPriceByUserId(insertUserId,7,view.getSettleId());
//		view.setYprice4(StringUtil.addDH(String.valueOf(getYPrice4.getYsPrice())));
//		view.setSprice4(StringUtil.addDH(String.valueOf(getYPrice4.getSsPrice())));
//		//变更交易票收费
//		TransferSettlementInfo getYPrice5 = new TransferSettlementInfo();
//		getYPrice5 = getYPrice5.getYPriceByUserId(insertUserId,5,view.getSettleId());
//		view.setYprice5(StringUtil.addDH(String.valueOf(getYPrice5.getYsPrice())));
//		view.setSprice5(StringUtil.addDH(String.valueOf(getYPrice5.getSsPrice())));
//		//流程表退费
//		TransferSettlementInfo getYPrice6 = new TransferSettlementInfo();
//		getYPrice6 = getYPrice6.getYPriceByUserId(insertUserId,2,view.getSettleId());
//		view.setYprice6(StringUtil.addDH(String.valueOf(getYPrice6.getYsPrice())));
//		view.setSprice6(StringUtil.addDH(String.valueOf(getYPrice6.getSsPrice())));
//		int ylctf = getYPrice6.getYsPrice();
//		int slctf = getYPrice6.getSsPrice();
//		//交易票退费
//		TransferSettlementInfo getYPrice7 = new TransferSettlementInfo();
//		getYPrice7 = getYPrice7.getYPriceByUserId(insertUserId,4,view.getSettleId());
//		view.setYprice7(StringUtil.addDH(String.valueOf(getYPrice7.getYsPrice())));
//		view.setSprice7(StringUtil.addDH(String.valueOf(getYPrice7.getSsPrice())));
//		ylctf = ylctf + getYPrice7.getYsPrice();
//		slctf = slctf + getYPrice7.getSsPrice();
//		//总计：
//		TransferSettlementInfo getYPrice8 = new TransferSettlementInfo();
//		getYPrice8 = getYPrice8.getTotlePriceByUserId(insertUserId,view.getSettleId());
//		view.setYprice8(StringUtil.addDH(String.valueOf(getYPrice8.getYsPrice()-ylctf*2)));
//		view.setSprice8(StringUtil.addDH(String.valueOf(getYPrice8.getSsPrice()-slctf*2)));
		
		view.setUserId(insertUserId);
		
		Codes code = Codes.getInstance();
		TransferSettlementInfoDao uDao = (TransferSettlementInfoDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_INFO);
		List<TransferSettlementInfoDto> list = uDao.getPriceByUserId(Integer.parseInt(insertUserId), view.getSettleId());
		int sumReceivable = 0;
		int sumPaid = 0;
		int sumReceivableBack = 0;
		int sumPaidBack = 0;
		for (int n = 0; n < list.size(); n++) {
			TransferSettlementInfoDto dto = list.get(n);
			dto.setChargeName(view.getChargeName());
			dto.setStartDate(view.getStartDate());
			dto.setEndDate(view.getEndDate());
			dto.setNum(n+1);
			dto.setSettleTypeName(code.getItemValueByItemIdAndItemCode(Codes.CHARGE_TYPE_CHARGE, dto.getSettletype()));
			if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
			sumReceivable = sumReceivable + dto.getReceivable();
			sumPaid = sumPaid + dto.getPaid();
			sumReceivableBack = sumReceivableBack + dto.getReceivableBack();
			sumPaidBack = sumPaidBack + dto.getPaidBack();
			dto.setReceivableStr(StringUtil.addDH((dto.getReceivable())));
			dto.setReceivableBackStr(StringUtil.addDH((dto.getReceivableBack())));
			dto.setPaidStr(StringUtil.addDH((dto.getPaid())));
			dto.setPaidBackStr(StringUtil.addDH((dto.getPaidBack())));
		}
		List<Codes> types = code.getCodesListByItemId(Codes.CHARGE_TYPE_CHARGE);
		Collections.sort(types, new Comparator<Codes>() {  
            @Override  
            public int compare(Codes o1, Codes o2) {  
                return o1.getItemCode()>o2.getItemCode() ? 1 : -1;  
            }  
        });  
		
		List<TransferSettlementInfoDto> viewList = new ArrayList<TransferSettlementInfoDto>();
		for (int n = 0; n < types.size(); n++) {
			Codes tmpCode = types.get(n);
			boolean f = true;
			for (TransferSettlementInfoDto dto : list) {
				if(dto.getSettletype() == tmpCode.getItemCode()) {
					dto.setNum(n+1);
					if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
					viewList.add(dto);
					f = false;
					break;
				} 
			}
			if(f) {
				TransferSettlementInfoDto dto = new TransferSettlementInfoDto();
				dto.setNum(n+1);
				dto.setChargeName(view.getChargeName());
				dto.setStartDate(view.getStartDate());
				dto.setEndDate(view.getEndDate());
				dto.setSettleTypeName(tmpCode.getItemValue());
				if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
				dto.setReceivable(0);
				dto.setReceivableBack(0);
				dto.setPaid(0);
				dto.setPaidBack(0);
				dto.setReceivableStr("0");
				dto.setReceivableBackStr("0");
				dto.setPaidStr("0");
				dto.setPaidBackStr("0");
				viewList.add(dto);
			}
		}
		
		view.setInfo(viewList);
		view.setSumReceivable(StringUtil.addDH((sumReceivable)));
		view.setSumReceivableBack(StringUtil.addDH((sumReceivableBack)));
		view.setSumPaid(StringUtil.addDH((sumPaid)));
		view.setSumPaidBack(StringUtil.addDH((sumPaidBack)));
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
